/*
 * @Author: HZK
 * @Date: 2020-10-19 19:14:58
 * @LastEditTime: 2020-10-20 19:02:15
 * @LastEditors: hanfengmi
 * @Description: 
 */
class Observer {
    constructor (data) {
        this.walk(data)
    }
    walk (data) {
        // 遍历所有属性
        if(!data || typeof data !== 'object'){
            return 
        }
        Object.keys(data).forEach(key => {
            this.defineReactive(data, key, data[key])
        })
    }

    defineReactive (obj, key, val) {
        this.walk(val)
        let that = this;
        // 
        let dep = new Dep()
        Object.defineProperty(obj, key, {
            enumerable:true,
            configurable:true,
            get() {
                Dep.target && dep.addSub(Dep.target)
                return val
            },
            set(newValue){
                if( newValue === val){
                    return 
                }
                val = newValue
                that.walk(newValue)
                dep.notify()
            }
        } )
    }
}